package com.dime.physical.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.alibaba.druid.sql.visitor.functions.If;
import com.dime.basic.api.RemoteBaseCombService;
import com.dime.basic.api.RemoteHarmService;
import com.dime.basic.api.domain.*;
import com.dime.basic.api.model.ComboBase;
import com.dime.physical.domain.DimeBasepackagecomb;
import com.dime.physical.domain.DimeBasepackageharm;
import com.dime.physical.domain.dto.PhysicalCombDto;
import com.dime.physical.mapper.DimeBasepackagecombMapper;
import com.dime.physical.mapper.DimeBasepackageharmMapper;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dime.physical.mapper.DimeBasepackageMapper;
import com.dime.physical.domain.DimeBasepackage;
import com.dime.physical.service.IDimeBasepackageService;

/**
 * 企业套餐管理Service业务层处理
 *
 * @author Tiny
 * @date 2023-09-14
 */
@Service
public class DimeBasepackageServiceImpl implements IDimeBasepackageService {
    @Autowired
    private DimeBasepackageMapper dimeBasepackageMapper;


    @Autowired
    private RemoteHarmService remoteHarmService;

    @Autowired
    private RemoteBaseCombService remoteBaseCombService;


    @Autowired
    private DimeBasepackageharmMapper dimeBasepackageharmMapper;

    @Autowired
    private DimeBasepackagecombMapper dimeBasepackagecombMapper;

    /**
     * 查询企业套餐管理
     *
     * @param id 企业套餐管理主键
     * @return 企业套餐管理
     */
    @Override
    public DimeBasepackage selectDimeBasepackageById(String id) {
        DimeBasepackage basepackage = dimeBasepackageMapper.selectDimeBasepackageById(id);
        List<PhysicalCombDto> physicalCombDtoList = new ArrayList<>();

        List<DimeBasepackagecomb> dimeBasepackagecombs = dimeBasepackagecombMapper.selectDimeBasepackagecombBypackageId(id);
        if (dimeBasepackagecombs != null && !dimeBasepackagecombs.isEmpty()) {
            for (DimeBasepackagecomb dimeBasepackagecomb : dimeBasepackagecombs) {
                PhysicalCombDto physicalCombDto = new PhysicalCombDto();
                physicalCombDto.setId(dimeBasepackagecomb.getDimeBasecombid());
                physicalCombDto.setCombName(dimeBasepackagecomb.getCombName());
                physicalCombDto.setCombCode(dimeBasepackagecomb.getCombCode());

                physicalCombDtoList.add(physicalCombDto);
            }
        }
        getPackageharmBypackageId(basepackage);
        basepackage.setPhysicalCombDtoList(physicalCombDtoList);

        return basepackage;
    }

    /**
     * 查询企业套餐管理列表
     *
     * @param dimeBasepackage 企业套餐管理
     * @return 企业套餐管理
     */
    @Override
    public List<DimeBasepackage> selectDimeBasepackageList(DimeBasepackage dimeBasepackage) {
        return dimeBasepackageMapper.selectDimeBasepackageList(dimeBasepackage);
    }

    /**
     * 新增企业套餐管理
     *
     * @param dimeBasepackage 企业套餐管理
     * @return 结果
     */
    @Override
    public int insertDimeBasepackage(DimeBasepackage dimeBasepackage) {

        dimeBasepackage.setId(IdUtils.fastUUID());
        dimeBasepackage.setCreateBy(SecurityUtils.getUsername());
        dimeBasepackage.setCreateTime(DateUtils.getNowDate());
        int row = dimeBasepackageMapper.insertDimeBasepackage(dimeBasepackage);
        insertDimeBasepackagecomb(dimeBasepackage);
        insertDimeBasepackageharm(dimeBasepackage);
        return row;
    }

    /**
     * 修改企业套餐管理
     *
     * @param dimeBasepackage 企业套餐管理
     * @return 结果
     */
    @Override
    public int updateDimeBasepackage(DimeBasepackage dimeBasepackage) {
        dimeBasepackage.setUpdateTime(DateUtils.getNowDate());
        dimeBasepackage.setUpdateBy(SecurityUtils.getUsername());
        dimeBasepackagecombMapper.deleteDimeBasepackagecombByPackageId(dimeBasepackage.getId());
        dimeBasepackageharmMapper.deleteDimeBasepackageharmBypackageid(dimeBasepackage.getId());
        insertDimeBasepackagecomb(dimeBasepackage);
        insertDimeBasepackageharm(dimeBasepackage);
        return dimeBasepackageMapper.updateDimeBasepackage(dimeBasepackage);
    }

    /**
     * 批量删除企业套餐管理
     *
     * @param ids 需要删除的企业套餐管理主键
     * @return 结果
     */
    @Override
    public int deleteDimeBasepackageByIds(String[] ids) {
        return dimeBasepackageMapper.deleteDimeBasepackageByIds(ids);
    }

    /**
     * 删除企业套餐管理信息
     *
     * @param id 企业套餐管理主键
     * @return 结果
     */
    @Override
    public int deleteDimeBasepackageById(String id) {
        return dimeBasepackageMapper.deleteDimeBasepackageById(id);
    }


    @Override
    public DimeBasepackage selectDimeBasepackageByCode(String packageCode) {

        DimeBasepackage dimeBasepackage = dimeBasepackageMapper.selectDimeBasepackageByCode(packageCode);
        getPackageharmBypackageId(dimeBasepackage);

        return dimeBasepackage;
    }

    private void getPackageharmBypackageId(DimeBasepackage dimeBasepackage) {

        List<DimeBasepackageharm> dimeBasepackageharms = dimeBasepackageharmMapper.selectDimeBasepackageharmByPackageId(dimeBasepackage.getId());
        if (dimeBasepackageharms != null && dimeBasepackageharms.size() > 0) {
            R<List<DimeBaseharm>> baseharmR = remoteHarmService.getHarmsByIds(dimeBasepackageharms.stream().map(DimeBasepackageharm::getDimeBaseharmid).toArray(String[]::new));
            if (baseharmR.getCode() != R.FAIL) {
                dimeBasepackage.dimeBaseharmList = baseharmR.getData();
            }
        }
        else
        {
            dimeBasepackage.dimeBaseharmList = new ArrayList<>();
        }
    }


    @Override
    public List<DimeBasepackage> selectDimeBasepackageByCompanyid(String companyid) {


        List<DimeBasepackage> dimeBasepackage = dimeBasepackageMapper.selectDimeBasepackBycompanyId(companyid);
        for (DimeBasepackage basepackage : dimeBasepackage) {

            //危害
            getPackageharmBypackageId(basepackage);

            //项目
            List<DimeBasepackagecomb> dimeBasepackagecombs = dimeBasepackagecombMapper.selectDimeBasepackagecombBypackageId(basepackage.getId());
            if (dimeBasepackagecombs != null && dimeBasepackagecombs.size() > 0) {

                String Codes = dimeBasepackagecombs.stream().map(DimeBasepackagecomb::getCombCode).collect(Collectors.joining(","));
                R<List<ComboBase>> basecombR = remoteBaseCombService.getCombosByCodes(Codes, SecurityConstants.INNER);
                if(basecombR.getCode() != R.FAIL)
                {
                    basepackage.physicalCombDtoList=new ArrayList<>();
                    for (ComboBase _combbase:basecombR.getData()) {

                        PhysicalCombDto combDto=new PhysicalCombDto();
                        combDto.setCombCode(_combbase.getCombCode());
                        combDto.setCombName(_combbase.getCombName());
                        combDto.setCombType(_combbase.getCombType());
                        combDto.setId(_combbase.getId());
                        combDto.setDimeGroupsid(_combbase.getGroupid());
                        combDto.setGroupName(_combbase.getGroupName());
                        basepackage.physicalCombDtoList.add(combDto);
                    }
                }
            }


        }
        return dimeBasepackage;


    }

    public void insertDimeBasepackagecomb(DimeBasepackage dimeBasepackage) {
        List<PhysicalCombDto> physicalCombDtoList = dimeBasepackage.getPhysicalCombDtoList();
        List<DimeBasecomb> combos = new ArrayList<>();

        if (physicalCombDtoList != null && !physicalCombDtoList.isEmpty()){
            R<List<DimeBasecomb>> Rcombos = remoteBaseCombService.getComboByIds(physicalCombDtoList.stream().map(PhysicalCombDto::getId).toArray(String[]::new));
            if (Rcombos.getCode() != R.FAIL) {
                combos = Rcombos.getData();
            }
        }
        if (combos != null && !combos.isEmpty()) {
            List<DimeBasepackagecomb> dimeBasepackagecombs = new ArrayList<>();
            for (DimeBasecomb combo : combos) {
                DimeBasepackagecomb dimeBasepackagecomb = new DimeBasepackagecomb();

                dimeBasepackagecomb.setId(IdUtils.fastUUID());
                dimeBasepackagecomb.setDimeBasecombid(combo.getId());
                dimeBasepackagecomb.setDimeBasepackageid(dimeBasepackage.getId());
                dimeBasepackagecomb.setCombName(combo.getCombName());
                dimeBasepackagecomb.setCombCode(combo.getCombCode());
                dimeBasepackagecomb.setCreateBy(SecurityUtils.getUsername());
                dimeBasepackagecomb.setCreateTime(DateUtils.getNowDate());

                dimeBasepackagecombs.add(dimeBasepackagecomb);
            }
            if (StringUtils.isNotEmpty(dimeBasepackagecombs)){
                dimeBasepackagecombMapper.batchDimeBasepackagecomb(dimeBasepackagecombs);
            }
        }
    }

    public void insertDimeBasepackageharm(DimeBasepackage dimeBasepackage) {
        List<DimeBaseharm> _dimeBaseharmList = dimeBasepackage.getDimeBaseharmList();
        List<DimeBaseharm> dimeBaseharmList = new ArrayList<>();

        if (_dimeBaseharmList!=null &&!_dimeBaseharmList.isEmpty()){
            R<List<DimeBaseharm>> baseharmR = remoteHarmService.getHarmsByIds(_dimeBaseharmList.stream().map(DimeBaseharm::getId).toArray(String[]::new));
            if (baseharmR.getCode() != R.FAIL) {
                dimeBaseharmList = baseharmR.getData();
            }
        }

        if (dimeBaseharmList != null && !dimeBaseharmList.isEmpty()) {
            List<DimeBasepackageharm> dimeBasepackageharms = new ArrayList<>();
            for (DimeBaseharm dimeBaseharm : dimeBaseharmList) {
                DimeBasepackageharm dimeBasepackageharm = new DimeBasepackageharm();
                dimeBasepackageharm.setId(IdUtils.fastUUID());
                dimeBasepackageharm.setDimeBaseharmid(dimeBaseharm.getId());
                dimeBasepackageharm.setDimeBasepackageid(dimeBasepackage.getId());
                dimeBasepackageharm.setHarmName(dimeBaseharm.getHamName());
                dimeBasepackageharm.setHarmCode(dimeBaseharm.getHamrCode());
                dimeBasepackageharm.setCreateBy(SecurityUtils.getUsername());
                dimeBasepackageharm.setCreateTime(DateUtils.getNowDate());

                dimeBasepackageharms.add(dimeBasepackageharm);
            }
            if (StringUtils.isNotEmpty(dimeBasepackageharms)){
                dimeBasepackageharmMapper.batchDimeBasepackageharm(dimeBasepackageharms);
            }
        }
    }
}
